import { errorCodeType } from "./errorCodeType";
export default {
  get(url, params, loading = true) {
    return this.request(url, params, "GET", loading);
  },
  post(url, params, loading = true) {
    return this.request(url, params, "POST", loading);
  },
  request(url, params, method, loading = true) {
    console.log('loading',loading);
    if (loading) {
      uni.showLoading({
        title: "加载中...",
        mask: true,
      });
    }
    return new Promise((resolve, reject) => {
      uni.request({
        url: url,
        data: params,
        header: {
          "content-type": "application/x-www-form-urlencoded", //自定义请求头信息
          Authorization: wx.getStorageSync("token"),
        },
        method: method,
        success: (res) => {
          uni.hideLoading();
          if (res.statusCode == 200) {
            resolve(res.data);
          } else {
            uni.showToast({
              title: errorCodeType(res.statusCode),
              icon: "none",
              duration: 2000,
            });
            reject(res.data);
          }
        },
        fail: (err) => {
          uni.hideLoading();
          reject(err);
        },
      });
    });
  },
};
